package com.ts.system.shiro;

import com.ts.animal.constant.AnimalConstant;
import com.ts.common.config.ApplicationContextRegister;
import com.ts.common.utils.ShiroUtils;
import com.ts.common.utils.StringUtils;
import com.ts.system.dao.UserDao;
import com.ts.system.domain.DeptDO;
import com.ts.system.domain.UserDO;
import com.ts.system.service.MenuService;
import com.ts.system.util.SystemUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class UserRealm extends AuthorizingRealm {
/*	@Autowired
	UserDao userMapper;
	@Autowired
	MenuService menuService;*/

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
        Long userId = ShiroUtils.getUserId();
        MenuService menuService = ApplicationContextRegister.getBean(MenuService.class);
        Set<String> perms = menuService.listPerms(userId);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setStringPermissions(perms);
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        Map<String, Object> map = new HashMap<>(16);

        String agentUsername = null;
        if (username.contains(AnimalConstant.super_username.concat(AnimalConstant.super_username_division))) {
            agentUsername = username.substring(AnimalConstant.super_username.length() + 1, username.length());
            username = AnimalConstant.super_username;
        }
        map.put("username", username);
        map.put("loginUserId", 1);
        String password = new String((char[]) token.getCredentials());

        UserDao userMapper = ApplicationContextRegister.getBean(UserDao.class);
        List<UserDO> list = userMapper.list(map);

        if (list.size() == 0) {
            throw new UnknownAccountException("账号不存在");
        }
        // 查询用户信息
        UserDO user = list.get(0);

        // 账号不存在
        if (user == null) {
            throw new UnknownAccountException("账号或密码不正确");
        }

        // 密码错误
        if (!password.equals(user.getPassword())) {
            throw new IncorrectCredentialsException("账号或密码不正确");
        }

        if (StringUtils.isNotEmpty(agentUsername)) {
            map.put("username", agentUsername);
            list = userMapper.list(map);
            if (list.size() > 0) {
                user = list.get(0);
            }
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, password, getName());
        SecurityUtils.getSubject().getSession().setAttribute("sysUser", user);
        return info;
    }

}
